/*
 * Copyright (C), 2015-2018
 * FileName: ApiJsonRep
 * Author:   zhao
 * Date:     2018/10/29 17:15
 * Description: api
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lizhaoblog.game.sbs.tmptouch.base.message;

import java.util.HashMap;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈api〉
 *
 * @author zhao
 * @date 2018/10/29 17:15
 * @since 1.0.1
 */
public class ApiJsonRep extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public static ApiJsonRep error() {
        return error(500, "未知异常，请联系管理员");
    }

    public static ApiJsonRep error(String msg) {
        return error(500, msg);
    }

    public static ApiJsonRep error(int code, String msg) {
        ApiJsonRep jsonRep = new ApiJsonRep();
        jsonRep.put("errno", code);
        jsonRep.put("errmsg", msg);
        return jsonRep;
    }

    public static ApiJsonRep success() {
        return new ApiJsonRep();
    }

    public static ApiJsonRep success(String msg) {
        ApiJsonRep jsonRep = new ApiJsonRep();
        jsonRep.put("errmsg", msg);
        return jsonRep;
    }

    private ApiJsonRep() {
        put("errno", 0);
        put("errmsg", "执行成功");
        put("data", new HashMap<String, Object>());
    }

    @Override
    public ApiJsonRep put(String key, Object value) {
        if ("errno".equals(key)) {
            super.put(key, value);
        } else if ("errmsg".equals(key)) {
            super.put(key, value);
        } else if ("data".equals(key)) {
            super.put(key, value);
        } else {
            ((Map<String, Object>) this.get("data")).put(key, value);
        }
        return this;
    }

}
